package com.zl.smartmall.library.listener;

import java.util.List;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.OrdersInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 我的订单列表请求监听类
 * @author 陈希然
 *
 */
public abstract class GetOrdersListener extends BaseListener {

	private static final String TAG = GetOrdersListener.class.getSimpleName();
	
	public GetOrdersListener(RequestCode requestCode) {
		super(requestCode);
	}
	
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		
			try {
				int status = response.getInt(Constants.REQ_STATUS);
				String msg = response.getString(Constants.REQ_MSG);
				CrashHandler.d(TAG, "我的订单列表服务器status = " + status);
				CrashHandler.d(TAG, "我的订单列表服务器msg = " + msg);
				
				switch(status){
				case Constants.REQ_OK:
					onResponseSuccess(OrdersInfo.parse(response.getJSONArray("result")));
					break;
				
				case Constants.REQ_OFFLINE:
					onOffline();
					break;
					
				case Constants.REQ_NO_MORE_DATA:
					onNoMoreData();
					break;
					
				default:
					onResponseFailed();
				}
				
			} catch (JSONException e) {
				onFailure(statusCode, headers, e, new JSONObject());
			}
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}
	/**
	 * 我的订单列表数据请求成功
	 */
	public abstract void onResponseSuccess(List<OrdersInfo> ordersInfos);
	
}
